home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 4 / BBS in a Box - Macintosh - Volume IV (January 1992) (BBS in a Box).iso / Files / Prog / A / Alpha.4.01.cpt / Alpha.4.01.rsrc / STR#_132.txt < prev    next >
Encoding:
Text File  |  1991-10-22  |  6.7 KB  |  337 lines

  1. ACMDs
  2.  
  3. Code resources of type 'ACMD' allow the user to add
  4.  
  5. modules of user-written code to Alpha. These modules
  6.  
  7. typically implement functionality that is too complex
  8.  
  9. for Alisp functions, or would be too slow. When an ACMD
  10.  
  11. is loaded, it is passed a pointer to a chunk of file
  12.  
  13. text (usually the selected text). The ACMD transforms
  14.  
  15. the text and returns a pointer to the transformed text
  16.  
  17. as a result. ACMDs can access Alpha flags and variables,
  18.  
  19. as well as define variables that Alpha maintains between
  20.  
  21. ACMD invocations. Example ACMDs included in this
  22.  
  23. distribution wrap text, insert the current data, and
  24.  
  25. play sounds.
  26.  
  27. 
  28.  
  29. The following is the way the 'main' routine of your ACMD
  30.  
  31. should be declared:
  32.  
  33. 
  34.  
  35.      /* pointer to a function that returns an 'int' */
  36.  
  37.      typedef int      (*FPtr)();
  38.  
  39. 
  40.  
  41.      char *main(text, alert, getVar, setVar, defVar,
  42.  
  43.                 delVar)
  44.  
  45.      char    *text;
  46.  
  47.      FPtr    alert, getVar, setVar, defVar, delVar;
  48.  
  49.      {
  50.  
  51.          :     :     :
  52.  
  53.         return (text);
  54.  
  55.      }
  56.  
  57. 
  58.  
  59. 'text' points to a null-terminated block of text
  60.  
  61. allocated with 'NewPtr'. Your routine must return a
  62.  
  63. similarly allocated block of text as its result. If the
  64.  
  65. two are not the same block, you are responsible for
  66.  
  67. deallocating the input block. Alpha will deallocate the
  68.  
  69. block of text returned by the ACMD. The exception is
  70.  
  71. when the ACMD returns NULL. In this case no deallocation
  72.  
  73. is performed. 'text' is usually a copy of the currently
  74.  
  75. selected text. However, macros can call ACMDs with other
  76.  
  77. selections.
  78.  
  79. 
  80.  
  81. Callback Functions
  82.  
  83. 
  84.  
  85. Each of the last five arguments is a pointer to a
  86.  
  87. callback function, an Alpha function that allows your
  88.  
  89. ACMD to interact with Alpha's internal variables or
  90.  
  91. display information.
  92.  
  93. 
  94.  
  95. The 'alert' parameter is a pointer to a function that
  96.  
  97. accepts the same arguments as the C 'printf' command,
  98.  
  99. displaying the result in an alert box.  Bear in mind
  100.  
  101. that Alpha is compiled with 16-bit integers, so if you
  102.  
  103. are calling Alert from a code resource that was compiled
  104.  
  105. with 32-bit ints, you must print your integers with
  106.  
  107. '%ld' instead of '%d'.
  108.  
  109. 
  110.  
  111. Each of the last four parameters is a pointer to an
  112.  
  113. Alpha function that your ACMD can call to read or modify
  114.  
  115. Alpha variables. Internally, Alpha prototypes these
  116.  
  117. functions as:
  118.  
  119. 
  120.  
  121.      /***********************************************
  122.  
  123.      *                                              *
  124.  
  125.      * Return the longword value of the variable    *
  126.  
  127.      * named by 'name' in 'val'. Works for either   *
  128.  
  129.      * Alpha predefined vars or those defined with  *
  130.  
  131.      * 'defineVar' below. Returns zero if the var   *
  132.  
  133.      * doesn't exist.                               *
  134.  
  135.      *                                              *
  136.  
  137.      ***********************************************/
  138.  
  139.      int getVar(char *name, long *val);
  140.  
  141. 
  142.  
  143. 
  144.  
  145.      /***********************************************
  146.  
  147.      *                                              *
  148.  
  149.      * Return the longword value of the variable    *
  150.  
  151.      * named by 'name'. Works for either Alpha      *
  152.  
  153.      * predefined vars or those defined with        *
  154.  
  155.      * 'defineVar' below. Returns 0 if the var      *
  156.  
  157.      * doesn't exist. To be sure that Think passes  *
  158.  
  159.      * a longword value for 'val', always typecast  *
  160.  
  161.      * 'val' to a long when you call this routine.  *
  162.  
  163.      *                                              *
  164.  
  165.      ***********************************************/
  166.  
  167.      int setVar(char *name, long val);
  168.  
  169. 
  170.  
  171. 
  172.  
  173.      /************************************************
  174.  
  175.      *                                               *
  176.  
  177.      * Define a longword variable named 'name' with  *
  178.  
  179.      * Initial value 'val'.                          *
  180.  
  181.      * Return 0 if the var already exists.           *
  182.  
  183.      *                                               *
  184.  
  185.      ************************************************/
  186.  
  187.      int defineVar(char *name, long val)
  188.  
  189. 
  190.  
  191. 
  192.  
  193.      /*******************************************
  194.  
  195.      *                                          *
  196.  
  197.      * Define a longword variable named 'name'. *
  198.  
  199.      * Return 0 if the variable doesn't exist.  *
  200.  
  201.      *                                          *
  202.  
  203.      *******************************************/
  204.  
  205.      int deleteVar(char *name);
  206.  
  207. 
  208.  
  209. 
  210.  
  211. These functions allow your ACMDs to behave differently
  212.  
  213. according to the current state of Alpha and the text you
  214.  
  215. are viewing. ACMDs can pass data among themselves by
  216.  
  217. defining their own variables. ACMD-defined variables can
  218.  
  219. also hold pointer values, allowing ACMDs to have
  220.  
  221. persistent storage. Note that while user variables and
  222.  
  223. Alpha string variables are long words, ordinary Alpha
  224.  
  225. variables such as 'indentOnCR' and 'regExpr' are shorts.
  226.  
  227. So even while you MUST pass in a longword value to set
  228.  
  229. an Alpha internal variable, only 16 bits are actually
  230.  
  231. used internally.
  232.  
  233. 
  234.  
  235. In summary, when an ACMD is called:
  236.  
  237. 
  238.  
  239.   ‚Ä¢ The selected code resource is loaded into memory
  240.  
  241.     and locked.
  242.  
  243. 
  244.  
  245.   ‚Ä¢ A new pointer is allocated with size equal to the
  246.  
  247.     size of the current selection + 1.
  248.  
  249. 
  250.  
  251.   ‚Ä¢ The selection (if any) is copied into the new
  252.  
  253.     pointer. The string will be null-terminated w/
  254.  
  255.     carriage returns to delimit lines.
  256.  
  257. 
  258.  
  259.   ‚Ä¢ The 'ACMD' resource is called as a 'C' function w/
  260.  
  261.     the text pointer and the function pointers as it's
  262.  
  263.     parameters.
  264.  
  265. 
  266.  
  267.   ‚Ä¢ The ACMD transforms the string, and returns it or
  268.  
  269.     NULL as it's result.
  270.  
  271. 
  272.  
  273.   ‚Ä¢ The original selection is replaced with the
  274.  
  275.     transformed text.
  276.  
  277. 
  278.  
  279.   ‚Ä¢ The ACMD resource is released.
  280.  
  281. 
  282.  
  283. Code resources of type 'ACMD' in Alpha's resource fork
  284.  
  285. are added to the ACMD menu under 'Utilities' and are
  286.  
  287. callable from macros (macros allow ACMD functions to be
  288.  
  289. bound to keystrokes). ACMDs can be added to Alpha's
  290.  
  291. resource fork by using ResEdit.
  292.  
  293. 
  294.  
  295. Since ACMDs are code resources, they must reference
  296.  
  297. their data off of register A4 instead of the usual A5.
  298.  
  299. From the standpoint of a C compiler like Think C, all
  300.  
  301. this means is that your main file must include the
  302.  
  303. header "SetUpA4.h", your program should begin with calls
  304.  
  305. to "RememberA0()" and "SetUpA4()", and it should end
  306.  
  307. with "RestoreA4()". MPW has similar setup routines. Note
  308.  
  309. that these routines use self-modifying code to store the
  310.  
  311. value of A4 on entry. Self-modifying code will not work
  312.  
  313. under A/UX, and probably not under system 8.0, whatever
  314.  
  315. decade that comes out. IM 6 has a workaround, for those
  316.  
  317. interested.
  318.  
  319. 
  320.  
  321. If the ACMD needs to link in library routines, care must
  322.  
  323. be taken to ensure that the libraries don't have any
  324.  
  325. global data references off of register A5. MacTraps of
  326.  
  327. ThinkC is fine. The ANSI library does declare globals,
  328.  
  329. but there is a version of the ANSI library that uses A4
  330.  
  331. instead of A5. Linking your code with MacTraps and
  332.  
  333. ANSI-A4 should take care of most of your library needs.
  334.  
  335. 
  336.  
  337.